import axios from "@/utils/requestdp";
import uuidv1 from "uuid/v1";
import dayjs from "dayjs";

let mixin = {
  props: {},
  mounted() {},
  data() {
    return {
      pointericon: {
        2: "geyazhan-01.png",
        4: "guolufang-01.png",
        5: "guolufang-01.png",
        6: "guolufang-01.png",
        7: "guolufang-01.png",
        1: "redianchang-01.png",
        3: "redianchang-01.png"
      },
    };
  },
  watch: {},
  methods: {

    //gis图 初始化方法
    //
    gongre() {
      //加载自定义流动纹理
      require("@/views/tongyongnn/ThreeDimensional/components/PolylineTrail");
      let _this = this;
      _this.$root.nv = {};
      this.getModelval("gisModal", res => {
        this.loaditemjsongiszzn(res);
      });
    },

    loaditemjsongiszzn(data) {
      data = JSON.parse(data)
      data = JSON.parse(data.data)
      let _this = this
      //判断线条data流向
      if (_this.mc.gisreverse) {
        data.features.map(item => {
          if (item.geometry.type == "LineString" && _this.mc.gisreverse.indexOf(Number(item.properties.pid)) != -1) {
            item.geometry.coordinates.reverse()
          }
        })
      }
      var viewer = this._earth.czm.viewer;
      var promise = Cesium.GeoJsonDataSource.load(data);
      promise.then(function(dataSource) {
        viewer.dataSources.add(dataSource);
        _this.ds.push(dataSource._name);
        _this.$root.dsgis = dataSource
        _this.$root.commonbtn.switchrelizhan1()
        _this.$root.commonbtn.switchguanjing1()
        var entities = dataSource.entities.values;
        for (var o = 0; o < entities.length; o++) {
          var entity = entities[o];

          //删除默认提示框
          entity.billboard = undefined;

          //判断点
          if (
            entity.id.split(".")[0] == "heatplant"
          ) {
            //特殊点
            //热源厂 需要区分隔压站 锅炉房 热源
            if (entity.properties.name._value != "") {
              // if (
              //   entity.properties.name._value.indexOf("T") == -1 &&
              //   entity.properties.name._value.indexOf("V") == -1
              // ) {
              if (
                true
              ) {
                let nameid = entity.id;
                let ln = Cesium.Cartographic.fromCartesian(
                  entity.position._value
                );
                _this.$root.nv[entity.id] = {
                  id: entity.id,
                  name: entity.properties.name._value,
                  location: [ln.longitude, ln.latitude, ln.height],
                  ln: ln
                };
                let uniqueID = uuidv1();
                if (
                  _this.mc.stop.indexOf(Number(entity.id.split(".")[1])) != -1
                ) {
                  entity.point = new Cesium.PointGraphics({
                    color: Cesium.Color.fromCssColorString("#f00d"),
                    pixelSize: 10 * _this.$store.state.daping.scale,
                    outlineWidth: 3,
                    outlineColor: Cesium.Color.fromCssColorString("#9EB8DF"),
                    distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
                      10.0,
                      52820.0
                    ),
                    scaleByDistance: new Cesium.NearFarScalar(
                      500,
                      1,
                      52820,
                      0.01
                    )
                    // translucencyByDistance: 100,
                    // disableDepthTestDistance: 100.0
                  });
                } else {
                  _this._earth.sceneTree.root.children.push({
                    ref: uniqueID,
                    czmObject: {
                      customProp: "ghjghj",
                      xbsjType: "Pin",
                      xbsjGuid: uniqueID,
                      name: nameid,
                      position: [ln.longitude, ln.latitude, ln.height],
                      scale: 0.375 * _this.$store.state.daping.scale,
                      // disableDepthTestDistance: 0,
                      imageUrl:
                        _this.mc.heatplant[entity.id.split(".")[1]].length ? ( _this.mc.heatplant[entity.id.split(".")[1]][1] ? JSON.parse(_this.mc.heatplant[entity.id.split(".")[1]][1])['iconUrl'] : '/configuration/logo/'+ _this.pointericon[_this.mc.heatplant[entity.id.split(".")[1]][0] * 1]) : '/configuration/logo/'+ _this.pointericon[_this.mc.heatplant[entity.id.split(".")[1]]]
                        ,
                      // _this.pointericon[entity.properties.s._value],
                      // _this.pointericon[entity.properties._type._value],
                      pinBuilder: {},
                      far: 1073741824
                    }
                  });
                  _this.pinarr.push(uniqueID);
                }
              }
            }
          } else if (entity.id.split(".")[0] == "substation") {
            //普通点------------------------------------------------------------------------------
            //热力站
            if (entity.id.split(".")[0] == "substation") {
              let ln = Cesium.Cartographic.fromCartesian(
                entity.position._value
              );
              _this.$root.nv[entity.id] = {
                id: entity.id,
                name: entity.properties.name._value,
                location: [ln.longitude, ln.latitude, ln.height],
                ln: ln
              };
              //判断视角高度
              //判断是否已经显示
              //普通点
              //------------------------------------++++++++++++++++++++++++++++++
              if (entity.properties.name._value != "") {
                //添加热力站名称
                // if (_this.$root.switchrelizhan) {
                //   entity.label = {
                //     text : entity.properties.name._value,
                //     font : _this.getscalepxcnum(18) + 'px sans-serif',
                //     pixelOffset : new Cesium.Cartesian2(0, _this.getscalepxcnum(-10)),
                //     fillColor: Cesium.Color.fromCssColorString("#A0c8DB"),
                //     // font: "normal " + this.getscalepxcnum(8) + "px MicroSoft YaHei",
                //     showBackground: true,
                //     backgroundColor: Cesium.Color.fromCssColorString("#0D2654"),
                //     scale: 1.0,
                //     backgroundPadding: new Cesium.Cartesian2(2 * _this.$store.state.daping.scale, 1.5 * _this.$store.state.daping.scale),
                //     distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
                //       10.0,
                //       4000.0 * _this.$store.state.daping.scale
                //     ),
                //     scaleByDistance: new Cesium.NearFarScalar(
                //       500.0,
                //       1.0,
                //       5000.0 * _this.$store.state.daping.scale,
                //       0.001
                //     )
                //   }
                // }
                if (
                  true
                ) {
                // if (
                //   entity.properties.name._value.indexOf("T") == -1 &&
                //   entity.properties.name._value.indexOf("V") == -1
                // ) {
                  if (
                    _this.mc.stop.indexOf(Number(entity.id.split(".")[1])) != -1
                  ) {
                    entity.point = new Cesium.PointGraphics({
                      color: Cesium.Color.fromCssColorString("#f00d"),
                      pixelSize: 10 * _this.$store.state.daping.scale,
                      outlineWidth: 0.75 * _this.$store.state.daping.scale,
                      outlineColor: Cesium.Color.fromCssColorString("#9EB8DF"),
                      distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
                        10.0,
                        52820.0
                      ),
                      scaleByDistance: new Cesium.NearFarScalar(
                        500,
                        1,
                        52820,
                        0.01
                      )
                      // translucencyByDistance: 100,
                      // disableDepthTestDistance: 100.0
                    });
                  } else {
                    entity.point = new Cesium.PointGraphics({
                      color: Cesium.Color.fromCssColorString("#5C93BF"),
                      pixelSize: 10 * _this.$store.state.daping.scale,
                      outlineWidth: 0.75 * _this.$store.state.daping.scale,
                      outlineColor: Cesium.Color.fromCssColorString("#9EB8DF"),
                      distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
                        10.0,
                        52820.0
                      ),
                      scaleByDistance: new Cesium.NearFarScalar(
                        500,
                        1,
                        52820,
                        0.01
                      )
                      // translucencyByDistance: 100,
                      // disableDepthTestDistance: 100.0
                    });
                  }
                }
              }
            }
          } else if (entity.id.split(".")[0] == "pipeline") {
            //-------------------------------------------------------------------------------------------------------------------------------
            //添加流动管线

            //管径信息显示label

            // if (_this.$root.switchguanjing) {
            //   entity.position = entity.polyline.positions._value[entity.polyline.positions._value.length/2]
            //   entity.label = {
            //     text : 'DN' + entity.properties.dn._value,
            //     // text : _this.dn[entity.properties.dn._value],
            //     font : _this.getscalepxcnum(15) + 'px sans-serif',
            //     pixelOffset : new Cesium.Cartesian2(_this.getscalepxcnum(20), 0),
            //     // disableDepthTestDistance: 0,
  
            //     fillColor: Cesium.Color.fromCssColorString("#FFFF2D"),
            //     showBackground: true,
            //     backgroundColor: Cesium.Color.fromCssColorString("#0D2654"),
            //     scale: 1.0,
            //     backgroundPadding: new Cesium.Cartesian2(2 * _this.$store.state.daping.scale, 1.5 * _this.$store.state.daping.scale),
  
            //     distanceDisplayCondition: new Cesium.DistanceDisplayCondition(
            //       10.0,
            //       4000.0 * _this.$store.state.daping.scale
            //     ),
            //     scaleByDistance: new Cesium.NearFarScalar(
            //       500,
            //       1,
            //       5000 * _this.$store.state.daping.scale,
            //       0.001
            //     )
            //   }
            // }

            if (
              _this.mc.outSource.indexOf(Number(entity.id.split(".")[1])) != -1
            ) {
              entity.polyline = {
                positions: entity.polyline.positions._value,
                width: 2.5 * _this.$store.state.daping.scale,
                material: new Cesium.PolylineTrailLinkMaterialProperty(
                  Cesium.Color.fromCssColorString(
                    _this.mc.draw[entity.id.split(".")[1]]
                      ? _this.mc.draw[entity.id.split(".")[1]]
                      : "#ddd"
                  ),
                  6000
                )
                // scaleByDistance: new Cesium.NearFarScalar(500, 5, 52820, 0.001),
              };
            } else {
              entity.polyline = {
                positions: entity.polyline.positions._value,
                width: 1.25 * _this.$store.state.daping.scale,
                material: new Cesium.PolylineTrailLinkMaterialProperty(
                  Cesium.Color.fromCssColorString(
                    _this.mc.draw[entity.id.split(".")[1]]
                      ? _this.mc.draw[entity.id.split(".")[1]]
                      : "#ddd"
                  ),
                  6000
                )
                // scaleByDistance: new Cesium.NearFarScalar(500, 5, 52820, 0.001),
              };
            }
          }
        }
      });
    },

    gongrepick(options) {
      //获取接口数据
      //数据返回显示弹窗
      //判断id
      let type = "substation";
      type =
        options.id.split(".")[0] == "heatplant" ? "heatplant" : "substation";
      let pid = options.pid;

      let requestBody = {
        // 热源
        queries: [
          {
            metric: "pid,name,m_001t,m_002t,m_001p,m_002p,m_001q,m_003qc",
            schema: "monitor"
          }
        ],
        table: "HeatPlant",
        search: "pid=" + pid
      };
      if (type == "substation") {
        requestBody = {
          // 热力站
          queries: [
            {
              metric:
                "pid,name,m_001t,m_002t,m_001p,m_002p,m_003q,m_003qc,m_003opening",
              schema: "monitor"
            }
          ],
          table: "SubStation",
          search: "pid=" + pid
        };
      }

      let requestBodyhis = {
        queries: [
          {
            metric: "pid,name,m_001t,m_002t,m_001p,m_002p",
            schema: "monitor"
          }
        ],
        table: "HeatPlant",
        search: "pid=" + pid,
        type: "history",
        orderBy: "aligntime",
        startTime: dayjs()
          .add(-3, "hour")
          .valueOf(),
        endTime: dayjs().valueOf()
      };

      if (type == "substation") {
        requestBodyhis = {
          // 热力站
          queries: [
            {
              metric: "pid,name,m_001t,m_002t,m_001p,m_002p",
              schema: "monitor"
            }
          ],
          table: "SubStation",
          search: "pid=" + pid,
          type: "history",
          orderBy: "aligntime",
          startTime: dayjs()
            .add(-3, "hour")
            .valueOf(),
          endTime: dayjs().valueOf()
        };
      }

      let reqnow = axios.post("/open/queryDb", requestBody);
      let reqhis = axios.post("/open/queryDb", requestBodyhis);
      options["data"] = {};

      Promise.all([reqnow, reqhis]).then(res => {
        if (
          res[0].code == 200 &&
          res[0].data.length != 0 &&
          res[1].code == 200 &&
          res[1].data.length != 0
        ) {
          let data = {};
          data["200"] = res[0].data;
          if (data["200"].length != 0) {
            if (type == "heatplant") {
              options["data"]["name"] = data["200"][0].name;
              options["data"]["gongwen"] = data["200"][0].m_001t
                ? data["200"][0].m_001t
                : "-";
              options["data"]["huiwen"] = data["200"][0].m_002t
                ? data["200"][0].m_002t
                : "-";
              options["data"]["gongya"] = data["200"][0].m_001p
                ? data["200"][0].m_001p
                : "-";
              options["data"]["huiya"] = data["200"][0].m_002p
                ? data["200"][0].m_002p
                : "-";
              options["data"]["liuliang"] = data["200"][0].m_001q
                ? data["200"][0].m_001q
                : "-";
              options["data"]["reliang"] = data["200"][0].m_003qc
                ? ((Number(data["200"][0].m_003qc) / 3.6)).toFixed(1)
                : "-";
            }

            if (type == "substation") {
              options["data"]["name"] = data["200"][0].name;
              options["data"]["gongwen"] = data["200"][0].m_001t
                ? data["200"][0].m_001t
                : "-";
              options["data"]["huiwen"] = data["200"][0].m_002t
                ? data["200"][0].m_002t
                : "-";
              options["data"]["gongya"] = data["200"][0].m_001p
                ? data["200"][0].m_001p
                : "-";
              options["data"]["huiya"] = data["200"][0].m_002p
                ? data["200"][0].m_002p
                : "-";
              options["data"]["liuliang"] = data["200"][0].m_003q
                ? data["200"][0].m_003q
                : "-";
              options["data"]["reliang"] = data["200"][0].m_003qc
                ? ((Number(data["200"][0].m_003qc) / 3.6) * 1000).toFixed(1)
                : "-";
              options["data"]["famenkaidu"] = data["200"][0].m_003opening
                ? data["200"][0].m_003opening
                : "-";
            }
          } else {
            options["data"]["name"] = data["200"][0].name;
            options["data"]["gongwen"] = "-";
            options["data"]["huiwen"] = "-";
            options["data"]["gongya"] = "-";
            options["data"]["huiya"] = "-";
            options["data"]["liuliang"] = "-";
            options["data"]["reliang"] = "-";
            options["data"]["famenkaidu"] = "-";
          }
          options["data"]["chartdata"] = res[1].data;
          let uniqueID = uuidv1();
          options.uid = uniqueID;
          options.type = type;
          this.dependlistArr.cardmsgzzn.push({
            uid: uniqueID,
            options: options
          });
        } else {
          this.$notify.info({
            message: "数据为空"
          });
        }
      });
    }
  }
};

export default mixin;
